<?php
/**
 * 站点主商户管理
 */
namespace logic\site;
use \logic\Base;
class SiteMainManage extends Base{
    
    
    /**
     * 管理员添加站点
     */
    public function adminAdd($site,$admin){
        $check = [    
            'domaintype'    => ['reg'=> [1,2,3]],
            'logo'          => ['check'=>'must','msg'=> '请上传站点logo图片'],
            'name'          => ['check'=>'must','msg'=> '请填写站点名称' ,'filter'=>1],
            'contactname'   => ['check'=>'must','msg'=> '请填写联系人姓名','filter'=>1],
            'contacttel'    => ['check'=>'must','msg'=> '请填写联系方式'],
            'provinceid'    => ['reg'=> 'int'],
            'cityid'        => ['reg'=> 'int'],
            'areaid'        => ['reg'=> 'int'],
            'streetid'      => ['reg'=> 'int'],
            'addressdetail' => ['filter'=>1],
        ];
        $save = pVerify($check,$site);        
        $save = pFilter()->replace('site',$save,$check);
        
        $adminInfo = $this->getUser()->getInfo();
        $msave = [
            'adduserid' => $adminInfo['agentid'],
            'opentype'  => 1,
            'level'     => 1,
            'name'      => $save['name'],
        ];
                
        $save['domain'] = \logic\site\SiteManage::createDomain($site['domain'],$site['domaintype']);
        $Area = new  \logic\sys\Area();
        $save['address'] = $Area->getFullAddress($save['provinceid'],$save['cityid'],$save['areaid'],$save['streetid']);
        $model = M();
        try{
            $model->startTrans();
            $mSiteid = D('SiteMain','site')->add($msave);
            $save['msiteid'] = $mSiteid;
            $siteid = D('Site','site')->add($save);
            D('Site','site')->edit($siteid,['domain'=>'s'.$siteid]);
            $admin['msiteid'] = $mSiteid;
            $AdminManage =  new \logic\site\AdminManage($this->CallUser);
            $AdminManage->add($admin);
            $model->commit();
            pFilter()->save('site',$siteid);
            return $siteid;
        }catch(\think\Exception $e){
            $model->rollBack();
            logicE($e->getMessage());
        }
    }
    
    
    
    
}